package vn.demand.dragon.view
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.core.Application;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.ReferenceProxy;
	import vn.demand.dragon.view.components.ProjectComplete;;
	
	public class ProjectCompleteMediator extends Mediator
	{
		
		public static var NAME:String = "ProjectCompleteMediator";
		
		private var projectProxy:ReferenceProxy;
		private var projectVO:Object;
		
		public function ProjectCompleteMediator(mediatorId:String)
		{
			super(mediatorId);
		}

		public function get completeForm():ProjectComplete {
			return viewComponent as ProjectComplete;
		}
		
		public function show(projectVO:Object, projectProxy:ReferenceProxy):void {
			this.projectVO = projectVO;
			this.projectProxy = projectProxy;
			
			var popup:ProjectComplete = PopUpManager.createPopUp(Application.application as DisplayObject, ProjectComplete, true) as ProjectComplete;
			setViewComponent(popup);
			
			completeForm.addEventListener('onRefresh', onRefresh);
			completeForm.addEventListener('onComplete', onComplete);
			completeForm.addEventListener('onCancel', onClose);
			
			completeForm.canComplete = false;
			
			projectProxy.complete(projectVO);
			
			completeForm.title = 'Complete reference: ' + projectVO.project_iso_code;
			PopUpManager.centerPopUp(popup);
		}
		
		public function onRefresh(event:Event):void {
			projectProxy.complete(projectVO);
			completeForm.canComplete = false;
			completeForm.messages.htmlText = '';
		}
		
		public function onComplete(event:Event):void {
			if (!doValidate()) {
				alert.error(errorStrings);
				return;
			}
			var df:DateFormatter = new DateFormatter();
        	df.formatString = 'YYYY-MM-DD';
        	var complete_date  = df.format(completeForm.complete_date.selectedDate);
			projectProxy.complete(projectVO, complete_date, 'DO_COMPLETE');
		}
		
		public function doValidate():Boolean {
			errorStrings = "";
			if (completeForm.complete_date.selectedDate == null) {
				errorStrings += "Please select [Complete date]\n";
			}
			return errorStrings.length == 0	? true : false;
		}
		
		public function onClose(event:Event):void {
			if (null != completeForm) {
				PopUpManager.removePopUp(completeForm);
			}
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	FFIAApplicationFacade.PROJECT_CAN_COMPLETE,
            	FFIAApplicationFacade.PROJECT_CAN_NOT_COMPLETE,
            	FFIAApplicationFacade.PROJECT_COMPLETED,
            	FFIAApplicationFacade.PROJECT_COMPLETE_FAILED
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
        	var o:Object = note.getBody();
            switch ( note.getName() ) 
			{
				case FFIAApplicationFacade.PROJECT_CAN_COMPLETE:
					completeForm.messages.htmlText = o.messages;
					completeForm.confirmLabel.visible = true; 
					completeForm.canComplete = true;
					break;
				case FFIAApplicationFacade.PROJECT_CAN_NOT_COMPLETE:
					completeForm.messages.htmlText = o.messages; 
					completeForm.confirmLabel.visible = false; 
					completeForm.canComplete = false;
					break;
				case FFIAApplicationFacade.PROJECT_COMPLETED:
					alert.info('Reference completed');
					onClose(new Event("closeProjectComplete"));
					break;
				case FFIAApplicationFacade.PROJECT_COMPLETE_FAILED:
					alert.error('Reference fail to complete with error: ' + o.messages);
					break;
                default:
                	break;
            }
        }
        
        private var errorStrings:String;
	}
}